package server.util;

import java.util.HashMap;
import java.util.Map;

//对hashMap的增加，删除，修改方法重写，只有在未上锁的情况下能修改内容，否则报错。
public final class ParameterMap<K, V> extends HashMap<K, V> {
    //是否上锁，未上锁可以读写，上锁后只能读
    private boolean locked = false;
    //构造器
    public ParameterMap() {
        super();
    }

    //
    @Override
    public void clear() {
        if (locked) {
            throw new IllegalStateException("No modifications are allowed to a locked ParameterMap");
        }
        super.clear();
    }

    @Override
    public V put(K key, V value) {
        if (locked)
            throw new IllegalStateException("No modifications are allowed to a locked ParameterMap");
        return super.put(key, value);
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> map) {
        if (locked)
            throw new IllegalStateException("No modifications are allowed to a locked ParameterMap");
        super.putAll(map);
    }

    @Override
    public V remove(Object key) {
        if (locked)
            throw new IllegalStateException("No modifications are allowed to a locked ParameterMap");
        return super.remove(key);
    }

    //getter
    public boolean isLocked() {
        return locked;
    }
    //setter
    public void setLocked(boolean locked) {
        this.locked = locked;
    }

}
